package topic;

import com.rabbitmq.client.*;
import utils.RabbitMQUtils;

import java.io.IOException;

public class Consumer1 {
    public static void main(String[] args) throws IOException {
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = null;
        if (connection != null) {
            channel = connection.createChannel();
        }
        //通道声明交换机
        String exchangeName = "topics";
        String exchangeType = "topic";
        if (channel != null) {
            channel.exchangeDeclare(exchangeName, exchangeType);
            //创建一个临时队列
            String queue = channel.queueDeclare().getQueue();
            //基于动态路由绑定队列和交换机
            String routingKey = "user.*";
            channel.queueBind(queue, exchangeName,routingKey);
            //消费消息
            channel.basicConsume(queue, true, new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) {
                    System.out.println("消费者-1："+new String(body));
                }
            });

        }



    }
}
